<template>
  <div>
    <el-header>
      店铺名称：
      <el-input v-model="DP" placeholder="请输入店铺名"></el-input>店铺地址
      <el-input v-model="DZ" placeholder="请输入地址"></el-input>分类：
      <el-input v-model="FL" placeholder="请输入分类"></el-input>
      <el-button type="primary" @click="add" @keydown.enter="add">添加</el-button>
    </el-header>
    <el-table
      :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      style="width: 100%"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="所属店铺">
              <span>{{ props.row.shop }}</span>
            </el-form-item>
            <el-form-item label="商品 ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="店铺 ID">
              <span>{{ props.row.shopId }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ props.row.category }}</span>
            </el-form-item>
            <el-form-item label="店铺地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="手机">
              <span>{{ props.row.phone }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column label="店铺名称" prop="name"></el-table-column>
      <el-table-column label="店铺地址" prop="address"></el-table-column>
      <el-table-column label="分类" prop="category"></el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入店铺名搜索" 
          @input="searchdata"/>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      small
      layout="total,prev,pager,next,jumper"
      :total="list.length"
      :page-size="pagesize"
      :pager-count="pagesize"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      search: "",
      DP: "", //店铺
      DZ: "", //地址
      FL: "", //分类
      pagesize: 5,
      currentPage: 1,
      data:[]
    };
  },
  computed: {
    list: function () {
      return this.$store.state.list;
    },
  },
  methods: {
    //删除
    handleDelete(index) {
      this.$store.dispatch("DEL_list", index);
    },
    //   添加
    add() {
      this.$store.dispatch("ADD_list", {
        name: this.DP,
        address: this.DZ,
        category: this.FL,
      });
      this.DP = "";
      this.DZ = "";
      this.FL = "";
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    searchdata(){
        // let list=this.$store.state.list
        // let temp=[]
        // list.forEach(element => {
        //     if(element.name.includes(this.search)){
        //         temp.push(element)
        //     }
        // });
        // list=temp
        this.$store.dispatch("search",this.search);
    }
  },
  mounted() {
    this.$store.dispatch("GET_list");
  },
};
</script>

<style scoped>
.el-input {
  width: 200px;
  text-align: left;
}
</style>